package simulator.ui;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.List;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import simulator.Scenario;
import simulator.software.Process;

/**
 * Modal dialog for displaying end-of-simulation statistics for process
 * scheduling.
 * 
 * @author Kyle Loomis
 * 
 */
public class StatisticsDialog extends JDialog
{
   public static final long serialVersionUID = 1L;
   private JFrame parent = null;
   private SimulationController controller = null;

   /**
    * Default constructor.
    * 
    * @param controller
    *           the simulation controller responsible for this dialog
    * @param parent
    *           the <code>JFrame</code> that this dialog should attach to
    */
   public StatisticsDialog(SimulationController controller, JFrame parent)
   {
      super(parent, true);

      this.controller = controller;
      this.parent = parent;

      setTitle("Results Summary");
      setLayout(new GridBagLayout());

      setSize(500, 400);
      setBackground(Color.WHITE);

      StringBuffer stats = new StringBuffer("<html>");

      Scenario scenario = controller.getScenario();
      List<Process> processTable = scenario.getComputer().getKernel()
            .getProcesses();

      stats.append("<b>CPU</b>");
      stats.append("<br />Time spent idle: "
            + controller.getScenario().getComputer().getKernel()
                  .getIdleCycles() + " cycles");
      stats.append("<br />Time spent handling interrupts: "
            + controller.getScenario().getComputer().getKernel()
                  .getInterruptCycles() + " cycles");
      stats.append("<br />Kernel time: "
            + controller.getScenario().getComputer().getKernel()
                  .getCyclesRunning() + " cycles");
      stats.append("<br /><br />");

      for (Process p : processTable)
      {

         stats.append("<b>").append(p.getName()).append("</b>");
         stats.append("<br />Time on CPU: " + p.getCyclesRunning() + " cycles");
         stats.append("<br />Time blocking: " + p.getCyclesBlocked()
               + " cycles");
         stats.append("<br />Completed on cycle " + p.getCycleCompleted());
         stats.append("<br /><br />");
      }

      JPanel statsPanel = new JPanel();
      statsPanel.setBackground(Color.white);
      statsPanel.setLayout(new GridBagLayout());
      JLabel statsLabel = new JLabel(stats.toString());

      statsPanel.add(statsLabel, new GridBagConstraints(0, 0, 1, 1, 1, 1,
            GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(
                  4, 4, 4, 4), 0, 0));

      JScrollPane pane = new JScrollPane(statsPanel);
      pane.setBackground(Color.white);

      GridBagConstraints constraints = new GridBagConstraints();
      constraints.fill = GridBagConstraints.BOTH;
      constraints.gridx = 0;
      constraints.gridy = 1;
      constraints.weightx = 0;
      constraints.weighty = 1;

      add(
            new JLabel(
                  "The scenario is complete and has compiled the following metrics."),
            new GridBagConstraints(0, 0, 1, 1, 1, 0,
                  GridBagConstraints.LINE_START, GridBagConstraints.BOTH,
                  new Insets(4, 4, 4, 4), 0, 0));

      add(pane, constraints);

      setVisible(true);
   }
}
